
package com.kanjian.music.adapter;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.dialog.MusicMenuDialog;
import com.kanjian.music.download.DownloadManager;
import com.kanjian.music.download.MusicDownloadTask;
import com.kanjian.music.entity.Music;
import com.kanjian.music.util.PlayerUtil;
import com.kanjian.music.volley.VolleyQueue;

public class MusicListListAdapater extends BaseAdapter {

    private BaseActivity mContext;

    private LayoutInflater mInflater;

    private ArrayList<Music> mDataList;

    private FragmentManager mFragmentManager;

    public MusicListListAdapater(BaseActivity context, ListView listView) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mFragmentManager = mContext.getSupportFragmentManager();
        if (listView != null) {
            setOnItemClickListener(listView);
        }
    }

    public MusicListListAdapater(BaseActivity context, ListView listView, ArrayList<Music> musicList) {
        this(context, listView);
        this.mDataList = musicList;
    }

    public MusicListListAdapater(BaseActivity context) {
        this(context, null);
    }

    public void setListView(ListView listView) {
        if (listView != null) {
            setOnItemClickListener(listView);
        }
    }

    private void setOnItemClickListener(ListView listView) {
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Music music = getItem(position);
                if (music != null) {
//                    MusicDownloadTask t = new MusicDownloadTask(music, 1, true);
//                    DownloadManager.addTask(t);
                    PlayerUtil.addMusicThenPlay(music, mContext.mPlayService);
                }
            }
        });
    }

    public void setMusicList(ArrayList<Music> musicList) {
        if (mDataList == null) {
            mDataList = new ArrayList<Music>(musicList.size());
        } else {
            mDataList.clear();
        }
        mDataList.addAll(musicList);
        notifyDataSetChanged();
    }

    public void setMusicList(List<Music> musicList) {
        if (mDataList == null) {
            mDataList = new ArrayList<Music>(musicList.size());
        } else {
            mDataList.clear();
        }
        mDataList.addAll(musicList);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDataList != null ? mDataList.size() : 0;
    }

    @Override
    public Music getItem(int position) {
        return mDataList != null ? mDataList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_list_musiclist, null);
            holder.mIv_musicPic = (NetworkImageView) convertView
                    .findViewById(R.id.item_list_musiclist_pic);
            holder.mIv_musicPic.setDefaultImageResId(R.drawable.default_image_small);
            holder.mIv_musicPic.setErrorImageResId(R.drawable.default_image_small);
            holder.mTv_musicName = (TextView) convertView
                    .findViewById(R.id.item_list_musiclist_musicname);
            holder.mTv_Songer = (TextView) convertView
                    .findViewById(R.id.item_list_musiclist_songer);
            holder.mBtn_menu = (Button) convertView.findViewById(R.id.item_list_musiclist_menu);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Music music = getItem(position);
        holder.mIv_musicPic.setImageUrl(music.getUserPic(), VolleyQueue.getImageLoader());
        holder.mTv_musicName.setText(music.getMusicName());
        holder.mTv_Songer.setText(music.getUserName());
        holder.mBtn_menu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicMenuDialog dialog = MusicMenuDialog.newInstance(music);
                dialog.show(mFragmentManager, "");
            }
        });
        return convertView;
    }

    static class ViewHolder {

        NetworkImageView mIv_musicPic;

        TextView mTv_musicName;

        TextView mTv_Songer;

        Button mBtn_menu;

    }

}
